<template>
  <div class="find">
    <h2>搜索页</h2>
    <van-search
      v-model="value"
      placeholder="请输入搜索关键词"
      @search="serchSong"
      @input="oninput"
    />
    <!-- 搜索下容器 -->
    <div class="search_wrap">
      <!-- 标题 -->
      <p class="hot_title">热门搜索</p>
      <!-- 热搜关键词容器 -->
      <div class="hot_name_wrap" v-if="songList.length == 0">
        <!-- 每个搜索关键词 -->
        <span
          class="hot_item"
          v-for="item in hot"
          :key="item.first"
          @click="hotClick(item)"
          >{{ item.first }}</span
        >
      </div>
      <!-- 搜索结果 -->
      <div class="search_wrap" v-else>
        <!-- 标题 -->
        <p class="hot_title">最佳匹配</p>
        <van-cell
          v-for="item in songList"
          :key="item.id"
          :title="item.name"
          size="large"
          :label="item.artists[0].name"
          @click="$router.push({ path: '/play', query: { id: item.id } })"
        >
          <template #right-icon>
            <van-icon name="play-circle-o" class="play-icon" />
          </template>
        </van-cell>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'find',
  data () {
    return {
      value: '', // 搜索关键词
      // 音乐数组
      songList: [],
      hot: []
    }
  },
  methods: {
    serchSong () {
      this.$axios({
        url: `/search?keywords= ${this.value}`,
        methods: 'get'
      }).then(res => {
        // console.log(res)
        this.songList = res.data.result.songs
      })
    },
    oninput () {
      if (this.value == '') {
        this.songList = []
      }
    },
    hotClick (item) {
      this.value = item.first
      this.serchSong()
    }
  },
  mounted () {
    this.$axios({
      url: '/search/hot'
    }).then(res => {
      this.hot = res.data.result.hots
    })
  }
}
</script>

<style scoped lang="less">
/* 搜索容器的样式 */
.search_wrap {
  padding: 0.266667rem;
}

/*热门搜索文字标题样式 */
.hot_title {
  font-size: 0.32rem;
  color: #666;
}

/* 热搜词_容器 */
.hot_name_wrap {
  margin: 0.266667rem 0;
}

/* 热搜词_样式 */
.hot_item {
  display: inline-block;
  height: 32px;
  margin-right: 8px;
  margin-bottom: 8px;
  padding: 0 14px;
  font-size: 14px;
  line-height: 32px;
  color: #333;
  border-color: #d3d4da;
  border-radius: 0.853333rem;
  border: 1px solid #d3d4da;
}

/* 给单元格设置底部边框 */
.van-cell {
  border-bottom: 1px solid lightgray;
}
.find .play-icon {
  font-size: 30px;
  color: gray;
}
.find {
  .van-cell__title {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}
</style>
